package com.axend.aerosense.login.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.fragment.MvvmBaseTitleFragment;
import com.axend.aerosense.base.impl.IOperationResult;
import com.axend.aerosense.base.impl.ISimpleAction;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.base.viewmodel.SimpleModelFactroy;
import com.axend.aerosense.common.base.IToolBar;
import com.axend.aerosense.common.bean.UserInfo;
import com.axend.aerosense.common.bean.VerificationBean;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.login.BR;
import com.axend.aerosense.login.R;
import com.axend.aerosense.login.databinding.LoginEnRegFragmentBinding;
import com.axend.aerosense.login.databinding.LoginEnUserInfoBinding;
import com.axend.aerosense.login.utils.LoginBtnTimeCountDown;
import com.axend.aerosense.login.utils.TextViewUtil;
import com.axend.aerosense.login.viewmodel.LoginUserInfoViewModel;
import com.blankj.utilcode.util.ToastUtils;


public class LoginEnRegFragment
        extends MvvmBaseFragment<LoginEnRegFragmentBinding, LoginUserInfoViewModel>
        implements ISimpleAction<UserInfo> {
    private LoginBtnTimeCountDown countDown;
    public static LoginEnRegFragment newInstance() {
        LoginEnRegFragment fragment = new LoginEnRegFragment();
//        Bundle args = new Bundle();
//        args.putSerializable(ROOM_TYPE, oldRoomType);
//        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();

    }


    private void initView() {
        viewDataBinding.loginInfoView.loginForgotPassword.setVisibility(View.GONE);
        try {
            TextViewUtil.initText(viewDataBinding.loginInfoView.loginUserAgreement);
        } catch (Exception e) {
            e.printStackTrace();
        }
        viewDataBinding.loginInfoView.loginButton.setText(R.string.login_register);
        viewDataBinding.loginInfoView.loginButton.setOnClickListener(v -> {
            if(!viewModel.isChecked.getValue()){
                ToastUtils.showShort(R.string.common_user_agreement_tips);
                return;
            }
            viewModel.onRegister(getActivity());
        });
        viewDataBinding.loginInfoView.loginCheckbox.setOnClickListener(v -> viewModel.changeCheck());
        viewDataBinding.loginInfoView.loginPasswordEye.setOnClickListener(v -> {
            Boolean isEyeOpen = viewModel.isEyeOpen.getValue();
            int pos = viewDataBinding.loginInfoView.loginPasswordTv.getSelectionStart();
            if (!isEyeOpen){
                viewDataBinding.loginInfoView.loginPasswordTv.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }else {
                viewDataBinding.loginInfoView.loginPasswordTv.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
            viewModel.changeEye();
            viewDataBinding.loginInfoView.loginPasswordTv.setSelection(pos);
        });

        viewDataBinding.loginInfoView.loginBtnGetCode.setOnClickListener(v->{
            viewModel.onEmailVCode(getActivity(), VerificationBean.ActionType.Register, new IOperationResult(){

                @Override
                public void onSuccess() {
                    countDown = new LoginBtnTimeCountDown(viewDataBinding.loginInfoView.loginBtnGetCode, 60);
                    countDown.btnStart();
                }

                @Override
                public void onFail() {

                }
            });
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (countDown != null){
            countDown.btnStop();
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.login_en_reg_fragment;
    }

    @Override
    public int getBindingVariable() {
        return BR.viewmdel;
    }

    @Override
    protected LoginUserInfoViewModel getViewModel() {
        SimpleModelFactroy<UserInfo> factroy = new SimpleModelFactroy<UserInfo>(this);
        LoginUserInfoViewModel tmp = new ViewModelProvider(this, factroy).get(LoginUserInfoViewModel.class);
        return tmp;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    public void onResume() {
        super.onResume();
        ((IToolBar)getActivity()).setToolBarVisibility(View.VISIBLE);
    }

    @Override
    public void onSuccess(UserInfo userInfo) {
        getActivity().finish();
    }

    @Override
    public void onFail(String decs) {

    }
}
